package com.alibaba.citrus.ots.sales.plan.functions.contract;

import com.alibaba.citrus.ots.common.util.SpringContextUtil;
import com.alibaba.citrus.ots.sales.plan.dto.api.request.CreateContractRequest;
import com.alibaba.citrus.ots.sales.plan.dto.api.request.PageQueryContractRequest;
import com.alibaba.citrus.ots.sales.plan.dto.api.request.QueryContractRequest;
import com.alibaba.citrus.ots.sales.plan.repository.ContractRepository;
import com.alibaba.citrus.ots.sales.plan.sdo.ContractSDO;
import com.alibaba.cz.base.tool.page.PageResult;
import lombok.Getter;
import lombok.Setter;

import java.util.List;

/**
 * @author wangyibing
 */
public class ContractEntity {
    @Getter
    @Setter
    private ContractSDO contractSDO;

    private static ContractRepository contractRepository;

    static {
        contractRepository = SpringContextUtil.getBean(ContractRepository.class);
    }

    public static ContractEntity of(ContractSDO contractSDO) {
        ContractEntity entity = new ContractEntity();
        entity.setContractSDO(contractSDO);
        return entity;
    }

    public String doCreate(CreateContractRequest createContractRequest) {
        return contractRepository.createContract(createContractRequest);
    }

    public PageResult<List<ContractSDO>> doPageQuery(PageQueryContractRequest pageQueryContractRequest) {
        return contractRepository.pageQuery(pageQueryContractRequest,true);
    }

    public ContractSDO doQuery(QueryContractRequest queryContractRequest) {
        return contractRepository.queryContractById(queryContractRequest);
    }

    public Integer doEdit() {
        return contractRepository.editContract(contractSDO);
    }

    public String save() {
        // 校验

        return contractRepository.save(this.contractSDO);
    }
}
